mui.init({
    swipeBack: false,
    pullRefresh: {
        container: "#refreshContainer", //下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
        down: {
            style: 'circle', //必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式
            color: '#2BD009', //可选，默认“#2BD009” 下拉刷新控件颜色
            height: '50px', //可选,默认50px.下拉刷新控件的高度,
            range: '100px', //可选 默认100px,控件可下拉拖拽的范围
            offset: '0px', //可选 默认0px,下拉刷新控件的起始位置
            auto: false, //可选,默认false.首次加载自动上拉刷新一次
            callback: function () {
                indexData.getModule('up');
                setTimeout(function () {
                    mui('#refreshContainer').pullRefresh().endPulldownToRefresh(false);
                }, 1000)
            }
        }
    }
});

var indexData;
mui.plusReady(function () {
    var api_host = plus.storage.getItem('HOST_URL');
    //5+环境下隐藏webview的滚动条。
    hideScrollBar();
    indexData = new Vue({
        el: '#data_VUE',
        data: {
            search_status:1,

            sliderBoxStatus:1,
            newsBoxStatus:1,
            questionBoxStatus:1,
            classBoxStatus:1,
            ebookBoxStatus:1,
            shopBoxStatus:1,

            slider_lists_status: 0,
            news_list_status: 0,
            tiku_list_status: 0,
            class_list_status: 0,
            book_list_status: 0,
            goods_list_status: 0,

            slider_lists: [],
            news_list: [],
            tiku_list: [],
            class_list: [],
            book_list: [],
            goods_list: [],
            auditStatus:'no',

            noticeStatus:0,
            noticeTitle:'',
        },
        watch:{
            auditStatus:function () {
                this.getModule();
            }
        },
        mounted() {
            var self = this;
            self.getModule();
            //获取当前版本审核状态
            getAudtitStatus(self);
            self.$nextTick(function () {
                self.getNoticeCon();
                var launchFlag = plus.storage.getItem("launchFlag");
                if(launchFlag){
                    ApplicationUpdateTool("update.html");
                    mui.later(function () {
                        if(mui.os.android && parseFloat(mui.os.version) < 6.0){
                            mui.toast("您的系统版本过低，可能无法正常使用部分功能，请升级系统版本之后再试", {duration: '10000',type:'div'});
                        }

                        if(mui.os.ios && parseFloat(mui.os.version) < 10.0){
                            mui.toast("您的系统版本过低，可能无法正常使用部分功能，请升级系统版本之后再试", {duration: '10000',type:'div'});
                        }
                    },2000)
                }
            });
        },
        methods: {
            getModule:function(type='normal'){
                var self = this;
                mui.getJSON("../../../manifest.json",null,function(data){
                    mui.ajax({
                        url: api_host + '/api/other/other/getAppModule',
                        dataType: 'json',
                        data:{
                            'versionNum': data.version.code,
                        },
                        type: 'get',
                        timeout:3000,
                        success: function (msg) {
                            if(msg.code == 1){
                                if(msg.data.search == 0){
                                    $('.top_search').css('display','none');
                                }else{
                                    $('.top_search').css('display','block');
                                }

                                if(msg.data.slider == 0){
                                    self.sliderBoxStatus = 0;
                                }else{
                                    self.sliderBoxStatus = 1;
                                    self.getSliderData(type);
                                }

                                if(msg.data.news == 0){
                                    self.newsBoxStatus = 0;
                                }else{
                                    self.newsBoxStatus = 1;
                                    self.getNewsData();
                                }

                                if(msg.data.question == 0){
                                    self.questionBoxStatus = 0;
                                }else{
                                    self.questionBoxStatus = 1;
                                    self.getQuestionData();
                                }

                                if(msg.data.ebook == 0){
                                    self.ebookBoxStatus = 0;
                                }else{
                                    self.ebookBoxStatus = 1;
                                    self.getEbookData();
                                }

                                if(msg.data.class == 0){
                                    self.classBoxStatus = 0;
                                }else{
                                    self.classBoxStatus = 1;
                                    self.getClassData();
                                }

                                if(msg.data.shop == 0){
                                    self.shopBoxStatus = 0;
                                }else{
                                    self.shopBoxStatus = 1;
                                    self.getGoodsData();
                                }
                            }else{
                                self.getIndexData();
                            }
                        },
                        error: function (xhr, type, errorThrown) {
                            self.getIndexData();
                        }
                    });
                })
            },
            getIndexData:function(){
                var self = this;
                var connectionStatus = plus.networkinfo.getCurrentType();
                if(connectionStatus == 0 || connectionStatus == 1){
                   // mui.toast('您的设备没有连接到可用的网络', {duration: '2000',});
                    return false;
                }else{
                    self.getSliderData();
                    self.getNewsData();
                    self.getQuestionData();
                    self.getClassData();
                    self.getEbookData();
                    self.getGoodsData();
                }
            },
            getSliderData: function (type='normal') {
                var self = this;
                mui.ajax({
                    url: api_host + '/api/other/other/index_slider',
                    dataType: 'json',
                    type: 'get',
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    timeout:3000,
                    success: function (msg) {
                        if (msg.code == 1) {
                            self.slider_lists = msg.data;
                            self.slider_lists_status = 1;
                            console.log(type);
                            if(type == 'normal'){
                                new Swiper('#swiper', {
                                	speed:1000,
                                    effect: 'fade',
                                    grabCursor: true,
                                    centeredSlides: true,
                                    slidesPerView: 'auto',
                                    loopedSlides: 3,
                                    loop : true,
                                    autoplay:{
                                    	delay:4000,
                                        stopOnLastSlide:false,
                                        disableOnInteraction:false
                                    },
                                    pagination: {
                                        el: '.swiper-pagination',
                                    },
                                });
                            }
                        } else {
                            mui.toast(msg.msg, {duration: '300'});
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        console.log('幻灯片数据获取失败');
                    }
                });
            },
            getNewsData:function(){
                var self = this;
                mui.ajax({
                    url: api_host + '/api/linxuan/news/recommend',
                    dataType: 'json',
                    type: 'get',
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    timeout:3000,
                    success: function (msg) {
                        if (msg.code == 1) {
                            self.news_list = msg.data;
                            self.news_list_status = 1;
                        } else {
                            mui.toast(msg.msg, {duration: '300'});
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        console.log('推荐资讯获取失败');
                    }
                });
            },
            getQuestionData:function(){
                var self = this;
                mui.ajax({
                    url: api_host + '/api/questionbank/category/newGetGroupCatList',
                    dataType: 'json',
                    type: 'get',
                    data: {
                        'type': 'index'
                    },
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    timeout:3000,
                    success: function (msg) {
                        if (msg.code == 1) {
                            self.tiku_list = msg.data;
                            self.tiku_list_status = 1;
                        } else {
                            mui.toast(msg.msg, {duration: '300'});
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        console.log('题库信息获取失败');
                    }
                });
            },
            getClassData:function(){
                var self = this;
                mui.ajax({
                    url: api_host + '/api/course/course/courseslist',
                    dataType: 'json',
                    data: {
                        'type': 'index',
                        'auditStatus':self.auditStatus,
                        'osName':plus.os.name,
                    },
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    timeout:3000,
                    type: 'get',
                    success: function (msg) {
                        if (msg.code == 1) {
                            self.class_list = msg.data;
                            self.class_list_status = 1;
                        } else {
                            mui.toast(msg.msg, {duration: '300'});
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        console.log('推荐课程获取失败');
                    }
                });
            },
            getEbookData:function(){
                var self = this;
                mui.ajax({
                    url: api_host + '/api/ebook/index/indexlist',
                    dataType: 'json',
                    type: 'get',
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    timeout:3000,
                    success: function (msg) {
                        if (msg.code == 1) {
                            self.book_list = msg.data;
                            self.book_list_status = 1;
                        } else {
                            mui.toast(msg.msg, {duration: '300'});
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        console.log('电子书数据获取失败');
                    }
                });
            },
            getGoodsData:function(){
                var self = this;
                mui.ajax({
                    url: api_host + '/api/shop/products/getProductsV116?size=4',
                    dataType: 'json',
                    type: 'get',
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    timeout:3000,
                    success: function (msg) {
                        if (msg.code == 1) {
                            self.goods_list = msg.data;
                            self.goods_list_status = 1;
                        } else {
                            mui.toast(msg.msg, {duration: '300'});
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        console.log('商城数据获取失败');
                    }
                });
            },
            getNoticeCon:function(){
                //获取公告层内容
                var self = this;

                    mui.ajax({
                        url: api_host + '/api/other/other/getIndexNotice',
                        dataType: 'json',
                        data:{'version_name':plus.os.name},
                        type: 'get',
                        success: function (msg) {
                            console.log(JSON.stringify(msg));
                            if (msg.code == 1) {
                                if(plus.storage.getItem('noticeStatus') == '1' || plus.storage.getItem('noticeStatus') == null){
                                    self.noticeStatus = 1;
                                    self.noticeTitle = msg.data.title;
                                }else if(plus.storage.getItem('noticeStatus') == '0'){
                                    console.log('关闭公告层的时间为：'+plus.storage.getItem('closeNoticeTime'));
                                    console.log('后台刷新公告的时间为：'+msg.data.refresh_time);
                                    if(plus.storage.getItem('closeNoticeTime') !== null && parseInt(plus.storage.getItem('closeNoticeTime')) < msg.data.refresh_time){
                                        self.noticeStatus = 1;
                                        self.noticeTitle = msg.data.title;
                                    }else{
                                        self.noticeStatus = 0;
                                    }
                                }
                            } else {
                                //mui.toast(msg.msg, {duration: '300'});
                            }
                        },
                        error: function (xhr, type, errorThrown) {

                        }
                    });
            },
            open_nes_list: function (dataID) {
                //打开资讯详情页面
                mui.openWindow({
                    url: '../news/detail.html',
                    extras: {
                        'newsid': dataID
                    },
                    createNew:false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            open_tiku_detail: function (dataID) {
                openOneTestPaperDetails(dataID);
            },
            open_class_list: function (dataID) {
                //打开课程详情页面
                mui.openWindow({
                    url: '../courseDetail/courseDetail.html',
                    extras: {
                        'cid': dataID
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            open_ebook_detail: function (dataID) {
                //打开电子书详情页面
                mui.openWindow({
                    url: '../eBook/eBookDetail.html',
                    extras: {
                        'bookid': dataID
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            open_zhuanlan_list: function () {
                //打开专栏页面
                mui.openWindow({
                    url: '../columns/columns.html',
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            open_tiku_list: function () {
                //打开题库页面
                mui.openWindow({
                    url: '../columns/questionBank.html',
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            open_shu_list: function () {
                //打开书城页面
                mui.openWindow({
                    url: '../columns/bookCity.html',
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            open_kc_list: function () {
                //打开课程
                mui.openWindow({
                    url: '../columns/course.html',
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            open_sc_list: function () {
                //打开商城
                mui.openWindow({
                    url: '../columns/mall.html',
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            open_mall_details: function (dataID) {
                //打开商品详情页
                mui.openWindow({
                    url: '../courseDetail/mallDetail.html',
                    extras: {
                        'product_id': dataID
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            goAnnounceDetail: function(){
            	mui.openWindow({
                    url: 'noticeDetail.html',
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            ggClose: function(){
                var self = this;
                mui.confirm('确定关闭公告吗？', '', function (e) {
                    if (e.index == 1) {
                        plus.storage.setItem('noticeStatus','0');
                        var timestamp = parseInt(new Date().getTime()/1000);
                        plus.storage.setItem('closeNoticeTime',timestamp.toString());
                        self.noticeStatus = 0;
                    } 
                    /*else {
                        mui.toast('取消关闭', {duration: '200',type: 'div'});
                    }*/
                })

            },
        }
    })
});

/*
    打开套题页面或者套题购买页面
    @param dataID 套题ID
    @param type  套题类型
  */
function openOneTestPaperDetails(dataID){
    var api_host = plus.storage.getItem('HOST_URL');
    mui.ajax({
        url: api_host + '/api/questionbank/choice_question/newCheckOnesTestPaperAnalysis',
        data: {
            'id': dataID,
        },
        headers:{'User-Token':plus.storage.getItem('UserToken')},
        timeout:3000,
        dataType: 'json',
        type: 'get',
        success: function (msg) {
            if (msg.code == 1) {
                mui.openWindow({
                    url: '../tiku/oneTestPaper.html',
                    extras: {
                        'catid': dataID
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            } else if (msg.code == 2){
                mui.openWindow({
                    url: '../tiku/oneTestPaperAnalysis.html',
                    extras: {
                        'catid': dataID
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            }else if (msg.code == 3){
                mui.toast(msg.msg, {duration: '2000',type: 'div'});
            }else if (msg.code == 4){
                mui.openWindow({
                    url: '../tiku/taotiOrder.html',
                    extras: {
                        'catid': dataID
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            }else{
                mui.toast(msg.msg, {duration: '2000',type: 'div'});
            }
        },
        error: function (xhr, type, errorThrown) {
            mui.toast('套题打开失败，请重试', {duration: '2000',type: 'div'});
        }
    });
}

mui('.top_search').on('tap','.search_box',function () {
    //打开搜索界面
    mui.openWindow({
        url: '../search/keywords.html',
        createNew: false,
        show: {
            autoShow: true,
            aniShow: 'slide-in-top',
            duration: 100
        },
        waiting: {
            autoShow: false,
        }
    });
})